<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/23
 * Time: 10:24
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Service\Admin\SupplyService;
use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;

class SupplyController extends BaseController
{
    protected $service;

    public function __construct(Request $request, SupplyService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 供应商列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listSupply()
    {
        $validator  =   Validator::make($this->request->post(), [
            'query' =>  'array'
        ], [
            'query.array'   =>  '搜索条件必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SUPPLY_LIST_ERROR, $validator->errors()->first());
        }

        $query  =   $this->request->post('query', []);
        $page   =   (int) $this->request->input('page', 1);
        $size   =   (int) $this->request->input('page_size', 20);
        [$flag, $result]    =   $this->service->supplyList($query, $page, $size);

        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SUPPLY_LIST_ERROR, $result);
        }
    }

    /**
     * 供应商总列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function totalSupply()
    {
        [$flag, $result]    =   $this->service->supplyTotal();

        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SUPPLY_LIST_ERROR, $result);
        }
    }

    /**
     * 供应商编辑
     * @return \Illuminate\Http\JsonResponse
     */
    public function editSupply()
    {
        $validator  =   Validator::make($this->request->post(), [
            'supply_id'     =>  'required|integer|exists:mini_supply,id',
            'supply_name'   =>  'required|string|max:30',
            'supply_desc'   =>  'string|max:500',
            'sorting'       =>  'required|integer'
        ], [
            'supply_id.required'    =>  '供应商ID不能为空',
            'supply_id.integer'     =>  '供应商排序必须为整数',
            'supply_id.exists'      =>  '供应商排序不存在',
            'supply_name.required'  =>  '供应商名称不能为空',
            'supply_name.string'    =>  '供应商名称必须为字符',
            'supply_name.max'       =>  '供应商名称过长',
            'supply_desc.string'    =>  '供应商描述必须为字符',
            'supply_desc.max'       =>  '供应商描述过长',
            'sorting.required'      =>  '供应商排序不能为空',
            'sorting.integer'       =>  '供应商排序必须为整数'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SUPPLY_EDIT_ERROR, $validator->errors()->first());
        }

        $supply_id  =   (int) $this->request->post('supply_id');
        $params     =   $this->request->post();
        [$flag, $result]    =   $this->service->supplyEdit($params, $supply_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SUPPLY_EDIT_ERROR, $result);
        }
    }

    /**
     * 供应商删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteSupply()
    {
        $validator  =   Validator::make($this->request->post(), [
            'supply_id' =>  'required|integer|exists:mini_supply,id'
        ], [
            'supply_id.required'   =>  '供应商ID不能为空',
            'supply_id.integer'    =>  '供应商ID必须为整数',
            'supply_id.exists'     =>  '供应商不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SUPPLY_DELETE_ERROR, $validator->errors()->first());
        }

        $supply_id  =   (int) $this->request->post('supply_id');
        [$flag, $result]    =   $this->service->supplyDelete($supply_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SUPPLY_DELETE_ERROR, $result);
        }
    }

    /**
     * 供应商添加
     * @return \Illuminate\Http\JsonResponse
     */
    public function addSupply()
    {
        $validator  =   Validator::make($this->request->post(), [
            'supply_name'   =>  'required|string|max:30',
            'supply_desc'   =>  'string|max:500',
            'sorting'       =>  'required|integer'
        ], [
            'supply_name.required'  =>  '供应商名称不能为空',
            'supply_name.string'    =>  '供应商名称必须为字符',
            'supply_name.max'       =>  '供应商名称过长',
            'supply_desc.string'    =>  '供应商描述必须为字符',
            'supply_desc.max'       =>  '供应商描述过长',
            'sorting.required'      =>  '供应商排序不能为空',
            'sorting.integer'       =>  '供应商排序必须为整数'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SUPPLY_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->supplyAdd($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SUPPLY_ADD_ERROR, $result);
        }
    }

}
